
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>线程管理 · book for nxos</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="nxos development team">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="smp.html" />
    
    
    <link rel="prev" href="sched.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://book-os.org" target="_blank" class="custom-link">bookos</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" >
            
                <span>
            
                    
                    NXOS文档中心
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" >
            
                <span>
            
                    
                    简介
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    NXOS 简介
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.2" >
            
                <span>
            
                    
                    快速上手
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.2.1" data-path="../../tutorial/quick-start.html">
            
                <a href="../../tutorial/quick-start.html">
            
                    
                    虚拟平台运行
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3" >
            
                <span>
            
                    
                    内核
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="../base/framework.html">
            
                <a href="../base/framework.html">
            
                    
                    整体框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="../base/start.html">
            
                <a href="../base/start.html">
            
                    
                    启动流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3" data-path="../mm/mm.html">
            
                <a href="../mm/mm.html">
            
                    
                    内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.3.1" data-path="../mm/buddy.html">
            
                <a href="../mm/buddy.html">
            
                    
                    buddy伙伴算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3.2" data-path="../mm/heap.html">
            
                <a href="../mm/heap.html">
            
                    
                    heapCache虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3.3" data-path="../mm/vmspace.html">
            
                <a href="../mm/vmspace.html">
            
                    
                    vmspace虚拟地址空间
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.4" data-path="sched.html">
            
                <a href="sched.html">
            
                    
                    调度管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.1.3.4.1" data-path="thread.html">
            
                <a href="thread.html">
            
                    
                    线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.4.2" data-path="smp.html">
            
                <a href="smp.html">
            
                    
                    SMP多核调度
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.5" data-path="../process/process.html">
            
                <a href="../process/process.html">
            
                    
                    进程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.5.1" data-path="../process/syscall.html">
            
                <a href="../process/syscall.html">
            
                    
                    系统调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.6" data-path="../interrupt/irq.html">
            
                <a href="../interrupt/irq.html">
            
                    
                    中断系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.6.1" data-path="../interrupt/delay_queue.html">
            
                <a href="../interrupt/delay_queue.html">
            
                    
                    延迟队列
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3.7" data-path="../timer/timer.html">
            
                <a href="../timer/timer.html">
            
                    
                    定时器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.4" >
            
                <span>
            
                    
                    内核移植
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.4.1" data-path="../port/riscv/riscv.html">
            
                <a href="../port/riscv/riscv.html">
            
                    
                    riscv架构移植
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.5" >
            
                <span>
            
                    
                    平台支持
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.5.1" data-path="../platform/i386.html">
            
                <a href="../platform/i386.html">
            
                    
                    i386
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.2" data-path="../platform/qemu_riscv64.html">
            
                <a href="../platform/qemu_riscv64.html">
            
                    
                    qemu_riscv64
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.3" data-path="../platform/k210.html">
            
                <a href="../platform/k210.html">
            
                    
                    k210
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.4" data-path="../platform/d1.html">
            
                <a href="../platform/d1.html">
            
                    
                    d1-nezha
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5.5" data-path="../platform/hifive_unmached.html">
            
                <a href="../platform/hifive_unmached.html">
            
                    
                    hifive unmached
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.6" >
            
                <span>
            
                    
                    用户态服务和驱动框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.6.1" data-path="../user/userver.html">
            
                <a href="../user/userver.html">
            
                    
                    用户态服务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6.2" data-path="../user/udriver.html">
            
                <a href="../user/udriver.html">
            
                    
                    用户态驱动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6.3" data-path="../user/ubpf.html">
            
                <a href="../user/ubpf.html">
            
                    
                    UBPF
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.7" >
            
                <span>
            
                    
                    用户接口
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.7.1" data-path="../user/api.html">
            
                <a href="../user/api.html">
            
                    
                    API设计
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.8" >
            
                <span>
            
                    
                    编程开发
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.8.1" data-path="../../tutorial/code-style.html">
            
                <a href="../../tutorial/code-style.html">
            
                    
                    代码风格
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.2" data-path="../../tutorial/commit-rule.html">
            
                <a href="../../tutorial/commit-rule.html">
            
                    
                    提交规则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.3" data-path="../../tutorial/menuconfig.html">
            
                <a href="../../tutorial/menuconfig.html">
            
                    
                    菜单配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.4" data-path="../../tutorial/toolchains.html">
            
                <a href="../../tutorial/toolchains.html">
            
                    
                    工具链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8.5" data-path="../../tutorial/utest.html">
            
                <a href="../../tutorial/utest.html">
            
                    
                    单元测试
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >线程管理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;">&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;</h1>
<h2 id="&#x4E00;-&#x63CF;&#x8FF0;">&#x4E00;. &#x63CF;&#x8FF0;</h2>
<p>&#x5185;&#x6838;&#x7684;&#x6700;&#x5C0F;&#x8C03;&#x5EA6;&#x5355;&#x4F4D;&#x662F;&#x7EBF;&#x7A0B;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x6709;&#x81EA;&#x5DF1;&#x72EC;&#x7ACB;&#x7684;&#x6808;&#xFF0C;&#x5BC4;&#x5B58;&#x5668;&#x7EC4;&#xFF0C;&#x4EE5;&#x53CA;&#x4E00;&#x4E9B;&#x8FD0;&#x884C;&#x4FE1;&#x606F;&#x7B49;&#x3002;
&#x4EE5;&#x7EBF;&#x7A0B;&#x4E3A;&#x6700;&#x5C0F;&#x8C03;&#x5EA6;&#x5355;&#x5143;&#x7684;&#x597D;&#x5904;&#x662F;&#x53EF;&#x4EE5;&#x5F88;&#x65B9;&#x4FBF;&#x652F;&#x6301;&#x5185;&#x6838;&#x6001;&#x4EE5;&#x53CA;&#x7528;&#x6237;&#x6001;&#x7684;&#x591A;&#x7EBF;&#x7A0B;&#xFF0C;&#x5E76;&#x4E14;&#x5C42;&#x6B21;&#x6846;&#x67B6;&#x4E5F;&#x6BD4;&#x8F83;&#x597D;&#xFF0C;&#x5BB9;&#x6613;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x5176;&#x5BF9;&#x5E94;&#x7684;&#x7EBF;&#x7A0B;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x4F7F;&#x7528;&#x5176;&#x6765;&#x8BB0;&#x5F55;&#x7EBF;&#x7A0B;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x6BD4;&#x5982;&#x7EBF;&#x7A0B;&#x94FE;&#x8868;&#xFF0C;&#x7EBF;&#x7A0B;&#x72B6;&#x6001;&#xFF0C;&#x7EBF;&#x7A0B;&#x6808;&#x7B49;&#x57FA;&#x7840;&#x4FE1;&#x606F;&#x3002;
&#x5728;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#x8BE5;&#x7EBF;&#x7A0B;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x7EBF;&#x7A0B;&#x53BB;&#x5B8C;&#x6210;&#x4E00;&#x4E9B;&#x72EC;&#x7ACB;&#x7684;&#x5DE5;&#x4F5C;&#x3002;&#x5185;&#x6838;&#x4E2D;&#x7684; <code>idle</code> &#x7EBF;&#x7A0B;&#xFF0C; <code>deamon</code> &#x7EBF;&#x7A0B;&#x7B49;&#x90FD;&#x6709;&#x81EA;&#x5DF1;&#x72EC;&#x7ACB;&#x7684;&#x5DE5;&#x4F5C;&#x4EFB;&#x52A1;&#x3002;</p>
<h2 id="&#x4E8C;-&#x539F;&#x7406;">&#x4E8C;. &#x539F;&#x7406;</h2>
<p>&#x5F53;&#x524D;&#x5B9E;&#x73B0;&#x7684;&#x7EBF;&#x7A0B;&#x7ED3;&#x6784;&#x4F53;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-c"><span class="hljs-keyword">struct</span> NX_Thread
{
    <span class="hljs-comment">/* thread list */</span>
    NX_List <span class="hljs-built_in">list</span>;
    NX_List globalList;
    NX_List exitList;
    NX_List processList;    <span class="hljs-comment">/* list for process */</span>

    NX_Spin lock;  <span class="hljs-comment">/* lock for thread */</span>

    <span class="hljs-comment">/* thread info */</span>
    NX_ThreadState state;
    NX_I32 tid;     <span class="hljs-comment">/* thread id, -1 means no alloc failed */</span>
    NX_ThreadHandler handler;
    <span class="hljs-keyword">void</span> *threadArg;
    <span class="hljs-keyword">char</span> name[NX_THREAD_NAME_LEN];

    <span class="hljs-comment">/* thread stack */</span>
    NX_U8 *stackBase;  <span class="hljs-comment">/* stack base */</span>
    NX_Size stackSize; 
    NX_U8 *<span class="hljs-built_in">stack</span>;      <span class="hljs-comment">/* stack top */</span>

    <span class="hljs-comment">/* thread sched */</span>
    NX_U32 timeslice;
    NX_U32 ticks;
    NX_U32 fixedPriority;  <span class="hljs-comment">/* fixed priority, does not change dynamically  */</span>
    NX_U32 priority;    <span class="hljs-comment">/* dynamic priority, or in the case of time-sharing scheduling priority will change dynamically */</span>
    NX_U32 needSched;
    NX_U32 isTerminated;
    NX_UArch onCore;        <span class="hljs-comment">/* thread on which core */</span>
    NX_UArch coreAffinity;  <span class="hljs-comment">/* thread would like to run on the core */</span>

    <span class="hljs-comment">/* thread resource */</span>
    NX_ThreadResource resource;
};
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> NX_Thread NX_Thread;
</code></pre>
<p>&#x5728;&#x7EBF;&#x7A0B;&#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#xFF0C;&#x6709;&#x94FE;&#x8868;&#xFF0C;&#x57FA;&#x7840;&#x4FE1;&#x606F;&#xFF0C;&#x6808;&#x4FE1;&#x606F;&#xFF0C;&#x8C03;&#x5EA6;&#x4FE1;&#x606F;&#xFF0C;&#x4EE5;&#x53CA;&#x5185;&#x6838;&#x7684;&#x8D44;&#x6E90;&#x3002;</p>
<p>&#x6700;&#x5F00;&#x59CB;&#xFF0C;&#x5185;&#x6838;&#x662F;&#x6CA1;&#x6709;&#x7EBF;&#x7A0B;&#x6982;&#x5FF5;&#x7684;&#xFF0C;&#x5904;&#x7406;&#x5668;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6267;&#x884C;&#x6D41;&#x3002;
&#x5728;&#x5B8C;&#x6210;&#x5185;&#x6838;&#x7684;&#x521D;&#x59CB;&#x5316;&#x540E;&#xFF0C;&#x4F1A;&#x521B;&#x5EFA; <code>idle</code> &#x7EBF;&#x7A0B;&#xFF0C;&#x6765;&#x4FDD;&#x8BC1;&#x5904;&#x7406;&#x5668;&#x81F3;&#x5C11;&#x80FD;&#x591F;
&#x62FF;&#x5230;1&#x4E2A;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#xFF0C;&#x907F;&#x514D;&#x5185;&#x6838;&#x8DD1;&#x98DE;&#x3002;
&#x5F53;&#x5904;&#x7406;&#x5668;&#x8C03;&#x7528; <code>NX_SchedToFirstThread</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x53BB;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6765;&#x8FD0;&#x884C;&#x3002;
&#x6B64;&#x65F6;&#xFF0C;&#x5904;&#x7406;&#x5668;&#x5C31;&#x8FDB;&#x5165;&#x7EBF;&#x7A0B;&#x73AF;&#x5883;&#xFF0C;&#x5F00;&#x542F;&#x7EBF;&#x7A0B;&#x8C03;&#x5EA6;&#x751F;&#x6DAF;&#x3002;</p>
<p>&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x540E;&#xFF0C;&#x5C31;&#x4F1A;&#x5207;&#x6362;&#x5230;&#x5176;&#x5B83;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x4E2A;&#x5207;&#x6362;&#x53EF;&#x80FD;&#x662F;&#x4E3B;&#x52A8;&#x7684;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x88AB;&#x52A8;&#x7684;&#x3002;&#x7EBF;&#x7A0B;&#x5728;&#x963B;&#x585E;&#xFF08;<code>block</code>&#xFF09;&#x548C;&#x8BA9;&#x51FA;&#xFF08;<code>yield</code>&#xFF09;&#x7684;&#x65F6;&#x5019;&#x662F;&#x5C5E;&#x4E8E;&#x7EBF;&#x7A0B;&#x4E3B;&#x52A8;&#x53D1;&#x8D77;&#x5207;&#x6362;&#x3002;&#x5728;
&#x4EA7;&#x751F;&#x65F6;&#x949F;&#x4E2D;&#x65AD;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x53BB;&#x51CF;&#x5C11;&#x7EBF;&#x7A0B;&#x7684;&#x65F6;&#x95F4;&#x7247;&#xFF0C;&#x65F6;&#x95F4;&#x7247;&#x4E3A;0&#x540E;&#xFF0C;&#x518D;&#x53BB;&#x5207;&#x6362;&#x7EBF;&#x7A0B;&#xFF0C;&#x8FD9;&#x662F;&#x5C5E;&#x4E8E;&#x7EBF;&#x7A0B;&#x88AB;&#x52A8;&#x5207;&#x6362;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x7EBF;&#x7A0B;&#x7684;&#x5207;&#x6362;&#x901F;&#x5EA6;&#x975E;&#x5E38;&#x5FEB;&#xFF0C;&#x4E00;&#x822C;&#x5728; <code>1ms~100ms</code> &#x5C31;&#x4F1A;&#x53D1;&#x8D77;&#x4E00;&#x6B21;&#x5207;&#x6362;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x5728;&#x4EBA;&#x770B;&#x6765;&#xFF0C;&#x5C31;&#x662F;&#x591A;&#x4E2A;&#x4EFB;&#x52A1;&#x5728;
&#x540C;&#x65F6;&#x8FD0;&#x884C;&#x3002;&#x6BD4;&#x5982;&#x4F60;&#x4E00;&#x8FB9;&#x542C;&#x6B4C;&#xFF0C;&#x4E00;&#x8FB9;&#x5199;&#x4EE3;&#x7801;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x97F3;&#x4E50;&#x64AD;&#x653E;&#x5668;&#x548C;&#x4EE3;&#x7801;&#x7F16;&#x8F91;&#x5668;&#x8FD9;&#x4E24;&#x4E2A;&#x4EFB;&#x52A1;&#x5728;&#x5FEB;&#x901F;&#x5207;&#x6362;&#x800C;&#x5F62;&#x6210;&#x7684;&#x6548;&#x679C;&#x3002;</p>
<p><img src="figures/multi-threading_principle.png" alt="&#x591A;&#x7EBF;&#x7A0B;&#x539F;&#x7406;"></p>
<p>&#x7EBF;&#x7A0B;&#x662F;&#x5177;&#x6709;&#x4F18;&#x5148;&#x7EA7;&#x7684;&#xFF0C;&#x4F18;&#x5148;&#x7EA7;&#x9AD8;&#x7684;&#x7EBF;&#x7A0B;&#x5148;&#x8FD0;&#x884C;&#xFF0C;&#x4F18;&#x5148;&#x7EA7;&#x4F4E;&#x7684;&#x7EBF;&#x7A0B;&#x540E;&#x8FD0;&#x884C;&#x3002;&#x5728; <code>nxos</code> &#x4E2D;&#xFF0C;&#x5206;&#x4E3A;&#x5B9E;&#x65F6;&#x4F18;&#x5148;&#x7EA7;&#x548C;&#x5206;&#x65F6;&#x4F18;&#x5148;&#x7EA7;&#x3002;&#x5B9E;&#x65F6;&#x4F18;&#x5148;&#x7EA7;&#x662F;&#xFF0C;&#x53EA;&#x8981;&#x6709;&#x9AD8;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#xFF0C;&#x5C31;&#x4E0D;&#x80FD;&#x8FD0;&#x884C;&#x4F4E;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#x3002;&#x800C;&#x5206;&#x65F6;&#x4F18;&#x5148;&#x7EA7;&#x662F;&#x9AD8;&#x4F4E;&#x4F18;&#x5148;&#x7EA7;&#x7EBF;&#x7A0B;&#x90FD;&#x80FD;&#x5F97;&#x5230;&#x8FD0;&#x884C;&#xFF0C;&#x53EA;&#x662F;&#x4F4E;&#x4F18;&#x5148;&#x7EA7;&#x7EBF;&#x7A0B;&#x8FD0;&#x884C;&#x7684;&#x65F6;&#x95F4;&#x66F4;&#x5C11;&#x800C;&#x5DF2;&#x3002;&#x4F18;&#x5148;&#x7EA7; <strong>[0, 6]</strong> &#x5C5E;&#x4E8E;&#x5206;&#x65F6;&#x4F18;&#x5148;&#x7EA7;&#xFF0C;&#x4F18;&#x5148;&#x7EA7; <strong>[7, MAX - 1]</strong> &#x5C5E;&#x4E8E;&#x5B9E;&#x65F6;&#x4F18;&#x5148;&#x7EA7;&#x3002;&#x5728;&#x4F7F;&#x7528;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x901A;&#x5E38;&#x4F7F;&#x7528;&#x5B8F;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x6570;&#x5B57;&#x3002;&#x4F18;&#x5148;&#x7EA7;&#x5B8F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-c">
<span class="hljs-comment">/* time-sharing priority */</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> NX_THREAD_PRIORITY_IDLE     0   <span class="hljs-comment">/* idle thread priority */</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> NX_THREAD_PRIORITY_LOW      1   <span class="hljs-comment">/* low level priority */</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> NX_THREAD_PRIORITY_NORMAL   3   <span class="hljs-comment">/* normal level priority */</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> NX_THREAD_PRIORITY_HIGH     6   <span class="hljs-comment">/* high level priority */</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> NX_THREAD_PRIORITY_RT_MIN (NX_THREAD_PRIORITY_HIGH + 1)     <span class="hljs-comment">/* real time min priority */</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> NX_THREAD_PRIORITY_RT_MAX (NX_THREAD_MAX_PRIORITY_NR - 1)   <span class="hljs-comment">/* real time max priority */</span></span>
</code></pre>
<h2 id="&#x4E09;&#x3001;&#x6846;&#x67B6;&#x56FE;">&#x4E09;&#x3001;&#x6846;&#x67B6;&#x56FE;</h2>
<p><img src="figures/thread_framework.png" alt="&#x591A;&#x7EBF;&#x7A0B;&#x6846;&#x67B6;"></p>
<h2 id="&#x56DB;-&#x63A5;&#x53E3;">&#x56DB;. &#x63A5;&#x53E3;</h2>
<p>&#x7EBF;&#x7A0B;&#x7684;&#x521B;&#x5EFA;&#x662F; <code>NX_ThreadCreate</code> , &#x4F20;&#x5165;&#x7EBF;&#x7A0B;&#x7684;&#x540D;&#x5B57; <code>name</code> &#xFF0C;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#x51FD;&#x6570; <code>handler</code> &#x4EE5;&#x53CA;&#x53C2;&#x6570; <code>arg</code> , &#x8FD8;&#x6709;&#x91CD;&#x8981;&#x7684;&#x4F18;&#x5148;&#x7EA7; <code>priority</code>&#xFF0C;&#x5373;&#x53EF;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x8FD4;&#x56DE;&#x7EBF;&#x7A0B;&#x7684;&#x5730;&#x5740;&#x3002;
&#x4F46;&#x662F;&#x6CE8;&#x610F;&#xFF0C;&#x6B64;&#x65F6;&#x53EA;&#x662F;&#x521B;&#x5EFA;&#x4E86;&#x7EBF;&#x7A0B;&#xFF0C;&#x8FD8;&#x6CA1;&#x6709;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function">NX_Thread *<span class="hljs-title">NX_ThreadCreate</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *name, NX_ThreadHandler handler, <span class="hljs-keyword">void</span> *arg, NX_U32 priority)</span></span>;
</code></pre>
<p>&#x9500;&#x6BC1;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x662F;&#x53EA;&#x9700;&#x8981;&#x4F20;&#x5165;&#x7EBF;&#x7A0B;&#x5730;&#x5740;&#x5373;&#x53EF;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x9500;&#x6BC1;&#x4E0D;&#x4EE3;&#x8868;&#x628A;&#x7EBF;&#x7A0B;&#x4ECE;&#x8FD0;&#x884C;&#x961F;&#x5217;&#x4E2D;&#x6458;&#x9664;&#xFF0C;&#x53EA;&#x662F;&#x91CA;&#x653E;&#x7EBF;&#x7A0B;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function">NX_Error <span class="hljs-title">NX_ThreadDestroy</span><span class="hljs-params">(NX_Thread *thread)</span></span>;
</code></pre>
<p>&#x5728;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x540E;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x628A;&#x7EBF;&#x7A0B;&#x7ED1;&#x5B9A;&#x5230;&#x67D0;&#x4E2A;&#x5904;&#x7406;&#x5668;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x5904;&#x7406;&#x5668;&#x4EB2;&#x548C;&#x6027;&#xFF0C;&#x4F7F;&#x5F97;&#x8BE5;&#x7EBF;&#x7A0B;&#x88AB;&#x7ED1;&#x5B9A;&#x5728;&#x6307;&#x5B9A;&#x7684;&#x7EBF;&#x7A0B;&#x4E0A;&#x8FD0;&#x884C;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function">NX_Error <span class="hljs-title">NX_ThreadSetAffinity</span><span class="hljs-params">(NX_Thread *thread, NX_UArch coreId)</span></span>;
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x597D;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#xFF0C;&#x8C03;&#x7528; <code>NX_ThreadStart</code> &#x5C31;&#x53EF;&#x4EE5;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#xFF0C;&#x7EBF;&#x7A0B;&#x5C31;&#x53EF;&#x4EE5;&#x5F00;&#x59CB;&#x8FD0;&#x884C;&#x4E86;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function">NX_Error <span class="hljs-title">NX_ThreadStart</span><span class="hljs-params">(NX_Thread *thread)</span></span>;
</code></pre>
<p>&#x6709;&#x65F6;&#x5019;&#x9700;&#x8981;&#x7EC8;&#x6B62;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x7684;&#x8FD0;&#x884C;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>NX_ThreadTerminate</code> &#x53BB;&#x7EC8;&#x6B62;&#x7EBF;&#x7A0B;&#xFF0C;&#x4F20;&#x5165;&#x7EBF;&#x7A0B;&#x6307;&#x9488;&#x5373;&#x53EF;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function">NX_Error <span class="hljs-title">NX_ThreadTerminate</span><span class="hljs-params">(NX_Thread *thread)</span></span>;
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x4E86;&#x81EA;&#x5DF1;&#x7684;&#x5DE5;&#x4F5C;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x4E3B;&#x52A8;&#x9000;&#x51FA;&#x8FD0;&#x884C;&#xFF0C;&#x8C03;&#x7528; <code>NX_ThreadExit</code> &#x51FD;&#x6570;&#x5373;&#x53EF;&#x9000;&#x51FA;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x7684;&#x8FD0;&#x884C;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">NX_ThreadExit</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;
</code></pre>
<p>&#x5F88;&#x591A;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x9700;&#x8981;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>NX_ThreadSelf</code> &#x83B7;&#x53D6;&#x81EA;&#x5DF1;&#x7684;&#x7EBF;&#x7A0B;&#x6307;&#x9488;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function">NX_Thread *<span class="hljs-title">NX_ThreadSelf</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x6709;&#x4E00;&#x4E2A;&#x8BA9;&#x51FA;&#x5F53;&#x524D;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5C31;&#x662F;&#x6682;&#x65F6;&#x4E0D;&#x5141;&#x8BB8;&#xFF0C;&#x5E0C;&#x671B;&#x628A;&#x8FD0;&#x884C;&#x673A;&#x4F1A;&#x8BA9;&#x7ED9;&#x5176;&#x5B83;&#x7EBF;&#x7A0B;&#xFF0C;&#x505A;&#x4E00;&#x4E2A;&quot;&#x597D;&#x4EBA;&quot;&#xFF0C;&#x6B64;&#x65F6;&#x8C03;&#x7528; <code>NX_ThreadYield</code> &#x5C31;&#x53EF;&#x4EE5;&#x8BA9;&#x51FA;&#x6267;&#x884C;&#x6743;&#x3002;&#x503C;&#x5F97;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x672C;&#x7EBF;&#x7A0B;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#xFF0C;&#x4F1A;&#x5728;&#x4E0B;&#x4E00;&#x4E2A;&#x65F6;&#x673A;&#x518D;&#x6B21;&#x88AB;&#x8C03;&#x5EA6;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">NX_ThreadYield</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;
</code></pre>
<p>&#x5F53;&#x7EBF;&#x7A0B;&#x9700;&#x8981;&#x4F11;&#x7720;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x4F20;&#x5165;&#x4F11;&#x7720;&#x7684;&#x65F6;&#x95F4;&#xFF0C; &#x5728; <code>nxos</code> &#x5185;&#x6838;&#x4E2D;&#xFF0C;&#x4F11;&#x7720;&#x7684;&#x5355;&#x4F4D;&#x662F;&#x6BEB;&#x79D2;&#xFF0C;&#x4F11;&#x7720; <code>microseconds</code> &#x65F6;&#x95F4;&#x957F;&#x5EA6;&#x540E;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x5524;&#x9192;&#xFF0C;&#x7EE7;&#x7EED;&#x5F80;&#x540E;&#x6267;&#x884C;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-function">NX_Error <span class="hljs-title">NX_ThreadSleep</span><span class="hljs-params">(NX_UArch microseconds)</span></span>;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="sched.html" class="navigation navigation-prev " aria-label="Previous page: 调度管理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="smp.html" class="navigation navigation-next " aria-label="Next page: SMP多核调度">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"线程管理","level":"1.1.3.4.1","depth":4,"next":{"title":"SMP多核调度","level":"1.1.3.4.2","depth":4,"path":"programing-manual/sched/smp.md","ref":"programing-manual/sched/smp.md","articles":[]},"previous":{"title":"调度管理","level":"1.1.3.4","depth":3,"path":"programing-manual/sched/sched.md","ref":"programing-manual/sched/sched.md","articles":[{"title":"线程管理","level":"1.1.3.4.1","depth":4,"path":"programing-manual/sched/thread.md","ref":"programing-manual/sched/thread.md","articles":[]},{"title":"SMP多核调度","level":"1.1.3.4.2","depth":4,"path":"programing-manual/sched/smp.md","ref":"programing-manual/sched/smp.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy BookOS-developers 2022","modify_label":"修订时间：","modify_format":"YYYY-MM-DD"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"nxos development team","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"book for nxos","language":"zh-hans","links":{"sidebar":{"bookos":"http://book-os.org"}},"gitbook":"3.2.3","description":"document for nxos kernel"},"file":{"path":"programing-manual/sched/thread.md","mtime":"2022-05-03T18:23:23.518Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-06T18:41:01.186Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

